package com.haoran.dwz.common.startup;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.stereotype.Component;

//@Component
public class DBTableChecker implements StartupJob {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public void run(ContextRefreshedEvent event) {
		logger.info("Init usm manager tables.");
		try {
			DataSource dataSource = event.getApplicationContext().getBean(DataSource.class);
			ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
			populator.addScript(new ClassPathResource("sql/usm-mysql.sql"));
			populator.populate(dataSource.getConnection());
			logger.info("Init usm manager tables success.");
		} catch (SQLException e) {
			logger.error("Init usm manager tables failture.", e);
		}
	}
}
